package 桌面程序;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Timer;
import java.util.TimerTask;

public class 倒计时 extends JFrame {
    public 倒计时() {
        setTitle("倒计时程序");
        setSize(520, 380);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setResizable(true);

        // 主面板，白色背景
        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
        mainPanel.setBackground(Color.WHITE);
        mainPanel.setBorder(BorderFactory.createEmptyBorder(24, 40, 24, 40));

        // 顶部可编辑大标题
        JTextField titleField = new JTextField("专注");
        titleField.setFont(new Font("微软雅黑", Font.BOLD, 32));
        titleField.setForeground(Color.BLACK);
        titleField.setHorizontalAlignment(JTextField.CENTER);
        titleField.setBorder(BorderFactory.createEmptyBorder(8, 0, 8, 0));
        titleField.setBackground(Color.WHITE);
        titleField.setMaximumSize(new Dimension(Integer.MAX_VALUE, 56));
        mainPanel.add(titleField);
        // 下划线分隔
        JSeparator titleSeparator = new JSeparator();
        titleSeparator.setForeground(new Color(180,180,180));
        titleSeparator.setMaximumSize(new Dimension(Integer.MAX_VALUE, 2));
        mainPanel.add(titleSeparator);
        mainPanel.add(Box.createVerticalStrut(12));

        // 当前时间
        JPanel nowPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 12, 0));
        nowPanel.setBackground(Color.WHITE);
        JLabel nowLabel = new JLabel("当前时间：");
        nowLabel.setFont(new Font("微软雅黑", Font.BOLD, 18));
        JLabel nowTime = new JLabel();
        nowTime.setFont(new Font("微软雅黑", Font.PLAIN, 18));
        nowPanel.add(nowLabel);
        nowPanel.add(nowTime);
        mainPanel.add(nowPanel);
        mainPanel.add(Box.createVerticalStrut(8));

        // 目标时间（时分秒输入）
        JPanel targetPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 12, 0));
        targetPanel.setBackground(Color.WHITE);
        JLabel targetLabel = new JLabel("目标时间：");
        targetLabel.setFont(new Font("微软雅黑", Font.BOLD, 20));
        JTextField hourField = new JTextField("00", 2);
        JTextField minField = new JTextField("01", 2);
        JTextField secField = new JTextField("00", 2);
        Font timeFont = new Font("Consolas", Font.BOLD, 28);
        hourField.setFont(timeFont);
        minField.setFont(timeFont);
        secField.setFont(timeFont);
        hourField.setHorizontalAlignment(JTextField.CENTER);
        minField.setHorizontalAlignment(JTextField.CENTER);
        secField.setHorizontalAlignment(JTextField.CENTER);
        hourField.setPreferredSize(new Dimension(54, 48));
        minField.setPreferredSize(new Dimension(54, 48));
        secField.setPreferredSize(new Dimension(54, 48));
        targetPanel.add(targetLabel);
        targetPanel.add(hourField);
        targetPanel.add(new JLabel(":"));
        targetPanel.add(minField);
        targetPanel.add(new JLabel(":"));
        targetPanel.add(secField);
        mainPanel.add(targetPanel);
        mainPanel.add(Box.createVerticalStrut(8));

        // 剩余时间（居中+自适应字体）
        JPanel remainPanel = new JPanel();
        remainPanel.setLayout(new BoxLayout(remainPanel, BoxLayout.X_AXIS));
        remainPanel.setBackground(Color.WHITE);
        JLabel remainLabel = new JLabel("剩余时间：");
        remainLabel.setFont(new Font("微软雅黑", Font.BOLD, 18));
        remainLabel.setAlignmentY(Component.CENTER_ALIGNMENT);
        JLabel remainTime = new JLabel("00时00分00秒", SwingConstants.CENTER);
        remainTime.setFont(new Font("微软雅黑", Font.BOLD, 36));
        remainTime.setForeground(Color.BLACK);
        remainTime.setAlignmentX(Component.CENTER_ALIGNMENT);
        remainTime.setHorizontalAlignment(SwingConstants.CENTER);
        remainPanel.add(Box.createHorizontalGlue());
        remainPanel.add(remainLabel);
        remainPanel.add(Box.createHorizontalStrut(10));
        remainPanel.add(remainTime);
        remainPanel.add(Box.createHorizontalGlue());
        mainPanel.add(remainPanel);
        mainPanel.add(Box.createVerticalStrut(18));

        // 按钮面板
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 24, 0));
        buttonPanel.setBackground(Color.WHITE);
        JButton startBtn = new JButton("开始");
        startBtn.setFont(new Font("微软雅黑", Font.BOLD, 20));
        startBtn.setBackground(Color.WHITE);
        startBtn.setForeground(Color.BLACK);
        startBtn.setFocusPainted(false);
        startBtn.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
        startBtn.setPreferredSize(new Dimension(120, 44));
        // 鼠标悬停高亮
        startBtn.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                startBtn.setBackground(new Color(230,230,230));
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                startBtn.setBackground(Color.WHITE);
            }
        });
        buttonPanel.add(startBtn);
        mainPanel.add(buttonPanel);

        add(mainPanel);

        // 定时更新时间
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                SwingUtilities.invokeLater(() -> {
                    nowTime.setText(new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date()));
                });
            }
        }, 0, 1000);

        // 动态调整剩余时间字体大小
        addComponentListener(new ComponentAdapter() {
            @Override
            public void componentResized(ComponentEvent e) {
                int panelWidth = remainPanel.getWidth();
                int panelHeight = remainPanel.getHeight();
                int fontSize = Math.max(36, Math.min(panelWidth / 8, panelHeight * 2 / 3));
                remainTime.setFont(new Font("微软雅黑", Font.BOLD, fontSize));
            }
        });

        // 倒计时逻辑
        startBtn.addActionListener(e -> {
            int h = 0, m = 0, s = 0;
            try {
                h = Integer.parseInt(hourField.getText().trim());
                m = Integer.parseInt(minField.getText().trim());
                s = Integer.parseInt(secField.getText().trim());
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(this, "请输入有效的时间！", "输入错误", JOptionPane.ERROR_MESSAGE);
                return;
            }
            int total = h * 3600 + m * 60 + s;
            if (total <= 0) {
                JOptionPane.showMessageDialog(this, "请输入大于0的时间！", "输入错误", JOptionPane.ERROR_MESSAGE);
                return;
            }
            startBtn.setEnabled(false);
            titleField.setEditable(false);
            hourField.setEditable(false);
            minField.setEditable(false);
            secField.setEditable(false);
            Timer countdown = new Timer();
            countdown.scheduleAtFixedRate(new TimerTask() {
                int remain = total;
                @Override
                public void run() {
                    if (remain <= 0) {
                        countdown.cancel();
                        SwingUtilities.invokeLater(() -> {
                            remainTime.setText("00时00分00秒");
                            startBtn.setEnabled(true);
                            titleField.setEditable(true);
                            hourField.setEditable(true);
                            minField.setEditable(true);
                            secField.setEditable(true);
                            Toolkit.getDefaultToolkit().beep();
                            JOptionPane.showMessageDialog(倒计时.this, "倒计时结束！", titleField.getText(), JOptionPane.INFORMATION_MESSAGE);
                        });
                    } else {
                        int rh = remain / 3600;
                        int rm = (remain % 3600) / 60;
                        int rs = remain % 60;
                        SwingUtilities.invokeLater(() -> {
                            remainTime.setText(String.format("%02d时%02d分%02d秒", rh, rm, rs));
                        });
                        remain--;
                    }
                }
            }, 0, 1000);
        });
    }

    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }
        SwingUtilities.invokeLater(() -> {
            new 倒计时().setVisible(true);
        });
    }
}
